package com.dongge;


import com.dongge.Entity.A;
import com.dongge.Entity.B;
import com.dongge.Entity.MyList;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Administrator on 2016/7/1.
 */
public class Main {


    @Test
    public void testHelloWorld(){

    }

    @Before
    public void printBefore(){

    }

    /**
     * 常规注入方法测试，使用
     * <bean id="a" class="com.dongge.Entity.A">
     * <property name="b" ref="b"/>
     * </bean>
     * <bean id="b" class="com.dongge.Entity.B">
     * </bean>
     */
    @Test
    public void testInject(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        A a = (A)applicationContext.getBean("a");
        a.print();
    }

    /**
     * 测试spring容器中对象是否相同，实际在默认情况下可以认为是单体
     */
    @Test
    public void testLifeCycle(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        A a = (A)applicationContext.getBean("a");
        A a1 = (A)applicationContext.getBean("a");
        System.out.println(a);
        System.out.println(a1);
    }

    /**
     * 使用factory-method static来获取对象
     * <bean id="b" class="com.dongge.Entity.FactoryStaticMethod" factory-method="getObjectB"/>
     */
    @Test
    public void getObjectByStaticMethod(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext1.xml");
        B b = (B)applicationContext.getBean("b");
        System.out.println(b);
    }

    /**
     * 使用对象的实例来创建对象
     * <bean id="inStanceMethod" class="com.dongge.Entity.InstanceMethod"/>
     *
     * <bean id="b" factory-bean="inStanceMethod" factory-method="getObjectB"/>
     */
    @Test
    public void getObjectByInstanceMethod(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext2.xml");
        B b = (B)applicationContext.getBean("b");
        System.out.println(b);
    }

    /**
     * 通过构造函数注入
     * <bean id="a" class="com.dongge.Entity.A">
     * <constructor-arg ref="b"/>
     * </bean>
     * <bean id="b" class="com.dongge.Entity.B">
     * </bean>
     */
    @Test
    public void injectByConstruction(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext3.xml");
        A a = (A)applicationContext.getBean("a");
        System.out.println(a);
    }

    /**
     * 注入一个列表数据
     * <bean id="myList" class="com.dongge.Entity.MyList">
     * <property name="names">
     * <list>
     * <value>dongge</value>
     * <value>zeus</value>
     * <value>abc</value>
     * </list>
     * </property>
     * </bean>
     */
    @Test
    public void injectAList(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext4.xml");
        MyList list = (MyList)applicationContext.getBean("myList");
        list.print();
    }

    /**
     * 测试AOP
     * 使
     */
    @Test
    public  void aopTest(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("aop.xml");
        com.dongge.aop.A a = (com.dongge.aop.A)applicationContext.getBean("a");
//        a.print();
        a.output();
    }

    /**
     * 使用args表达式来获取连接点变量名
     * args(name) 这里我们只提供了名字 而没有提供类型
     */
    @Test
    public void aopArgs(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("aop.xml");
        com.dongge.aop.A a = (com.dongge.aop.A)applicationContext.getBean("a");
        a.setName("zeus");
    }

    /**
     * 测试自定义的注解
     */
    @Test
    public void aopAnnotation(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("aop.xml");
        com.dongge.aop.A a = (com.dongge.aop.A) applicationContext.getBean("a");
        a.update();
    }

    @Test
    public void jdkProxy(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("aop.xml");
        com.dongge.aop.UseA useA = (com.dongge.aop.UseA) applicationContext.getBean("useA");
        useA.print();
    }



}
